package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.dom4j.rule.Mode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    /**
     * Spring容器和SpringMVC容器是有层次的（父子容器）
     * Spring容器：service对象+dao对象
     * SpringMVC容器：controller对象，，，，可以引用到Spring容器中的对象
     */


    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/queryAll")
    public String queryAll(Model model) throws Exception {
        List<Resume> list =  resumeService.queryResumeList();
        model.addAttribute("resume_list",list);
        return "resume_list";
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public String delete(@PathVariable Long id) throws Exception {
        resumeService.deleteResume(id);
        return "1";
    }

    @GetMapping("/{id}")
    public String detail(@PathVariable Long id, Model model) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        Resume resume;
        //id传-1表示新增操作
        if (id == -1){
            resume = new Resume();
        }else {
            resume = resumeService.detail(id);
        }
        model.addAttribute("resume",resume);
        return "resume_edit";
    }

    @PostMapping("")
    @ResponseBody
    public String update(Resume resume) throws Exception {
        resumeService.updateResume(resume);
        return "1";
    }

}
